﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using IDotNet.MapLibs.Model;

namespace IDotNet.MapLibs.BMap
{
    public class BMapGeocoding : IGeocoding
    {
        public IGeocoderResultModel Geocoder(IGeocoderRequestModel model)
        {
            var param = (BMapGeocoderRequestModel)model;
            if (model.address.IsNullOrWhiteSpace())
            {
                throw new MapLibException("param address is null");
            }
            var parms = new NameValueCollection
           {
               {"ak",param.ak??MapLibConfig.BMap.GeocodingAk},
               {"city",param.city},
               {"output",param.output},
               {"address",param.address},
           };
            var res = MapLibHelper.GetHttpResult(MapLibConfig.BMap.GeocodingUri, parms,method:"get");
            return param.output == "json" ? res.ToDeserializeJson<BMapGeocoderResultModel>() : res.ToDeserializeXml<BMapGeocoderResultModel>();
        }


        public IGeocoderLocationResultModel GeocoderLocation(IGeocoderLocationRequestModel model)
        {
            var param = (BMapGeocoderLocationRequestModel)model;
            var parms = new NameValueCollection
           {
               {"ak",param.ak??MapLibConfig.BMap.GeocodingAk},
               {"CoordType",param.CoordType.ToString()},
               {"output",param.output},
               {"location",param.location.ToLatLng()},
           };
            var res = MapLibHelper.GetHttpResult(MapLibConfig.BMap.GeocodingUri, parms);
            return param.output == "json" ? res.ToDeserializeJson<BMapGeocoderLocationResultModel>() : res.ToDeserializeXml<BMapGeocoderLocationResultModel>();

        }
    }
}
